﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FmcWebsite.Core;
using ContentManagementAdministrator.Dialogs;

namespace ContentManagementAdministrator
{
    public partial class UserControlAuthor : UserControl, ITab
    {
        private List<Author> author_list = new List<Author>();
        Multilang m_name = new Multilang("");
        Multilang m_desc = new Multilang("");
        Author author;
        string language = "ID";

        public UserControlAuthor()
        {
            InitializeComponent();

            // setting up tab languages
            tabPage1.Tag = "ID";
            tabPage2.Tag = "NL";
            tabPage3.Tag = "EN";


            imageList1.Images.Clear();
            listViewAuthors.Items.Clear();
            author_list.AddRange(Author.GetAll());

            foreach (Author a in author_list)
            {
                imageList1.Images.Add(Thumbnailer.CreateThumbnail(a.Picture, new Size(100, 100)));
                var item = listViewAuthors.Items.Add(Multilang.Extract(a.Name));
                item.Tag = a;
                item.ImageIndex = imageList1.Images.Count - 1;
            }

            

        }

        #region ITab Members

        public string GetTitle()
        {
            return "Authors";
        }

        #endregion

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            language = (string)tabControl1.SelectedTab.Tag;
            panel1.Parent = tabControl1.SelectedTab;
            textBoxName.Text = m_name[language];
            textBoxDesc.Text = m_desc[language];
        }

        private void listViewAuthors_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.author = null;
            if (listViewAuthors.SelectedItems.Count > 0)
            {
                Author a = (Author)listViewAuthors.SelectedItems[0].Tag;

                m_name.Value = a.Name;
                m_desc.Value = a.Description;

                textBoxName.Text = m_name[language];
                textBoxDesc.Text = m_desc[language];
                pictureBox1.Image = a.Picture;

                panelEdit.BackColor = Color.LightGreen;

                this.author = a;
            }
        }

        private void textBoxName_TextChanged(object sender, EventArgs e)
        {
            m_name[language] = textBoxName.Text;
            panelEdit.BackColor = Color.LightPink;
        }

        private void textBoxDesc_TextChanged(object sender, EventArgs e)
        {
            m_desc[language] = textBoxDesc.Text;
            panelEdit.BackColor = Color.LightPink;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            listViewAuthors_SelectedIndexChanged(null, null);
            panelEdit.BackColor = Color.LightGreen;
        }

        private void buttonApply_Click(object sender, EventArgs e)
        {
            if (author != null)
            {
                author.Name = m_name.Value;
                author.Description = m_desc.Value;
                author.Picture = pictureBox1.Image;
                author.Update();
                listViewAuthors.SelectedItems[0].Text = Multilang.Extract(author.Name);
            }
            panelEdit.BackColor = Color.LightGreen;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            FormLoadImage form = new FormLoadImage();

            if (form.Execute(new Size(200, 200)))
            {
                pictureBox1.Image = form.Image;
            }
        }
    }
}
